/*
editor.js

The MIT License

Copyright (c) 2011 Yuya.Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// エディタの基底

//! エディタ基底クラス
function EspEditorBase(name, width, height) {
	this.getName = function(){
		return name;
	}
	this.isOpen = false;
	this.width = width;
	this.height = height;
	this.window;
}
//! 編集用タグに設定するID
EspEditorBase.prototype.getDefaultTagID = function(){
	return "doc";
}

//! ヘッダの情報
EspEditorBase.prototype.getHeadMeta = function(){
	var text = "";
	text += "<link rel='stylesheet' href='css/develop.css'　type='text/css'>";
	return text;
}

//! タグを取得する
EspEditorBase.prototype.makeTag = function(text){
	return "<div id='"+this.getDefaultTagID()+"'>" + text + "</div>";
}

//! ドキュメントを書き出す、追記は出来ない
EspEditorBase.prototype.writedoc = function(text){
	var tag = this.window.document.getElementById(this.getDefaultTagID());
	if(tag){
		tag.innerHTML = this.makeTag(text);
	}
}

//! open後に呼び出すコールバック
EspEditorBase.prototype.afterOpen = function(){
	this.redraw();
}

//! 再描画
EspEditorBase.prototype.redraw = function(){
}

//! ウィンドウの開閉状態を取得する
EspEditorBase.prototype.getIsOpen = function(){
	return this.isOpen;
}

//! ウィンドウを開く
EspEditorBase.prototype.open = function(){
	if (this.isOpen == true) {
		return;
	}
	this.isOpen = true;
	this.window = window.open("", this.getName(), "width=" + this.width + ",height=" + this.height + ",status=1,resizable=1,scrollbars=1");
	// 生成したwindowでこのクラスを使用するためにwindowにthisをもたせる
	this.window.subcomponent = this;
	this.window.document.write("<html><head><title>"+this.getName()+"</title>"+this.getHeadMeta()+"</head><body class='develop'>"+this.makeTag("")+"</body></html>");
	// 生成したウィンドウのクローズにフック
	this.window.onunload = function () {
		this.isOpen = false;
	}
	this.afterOpen();
}
//! windowを閉じる
EspEditorBase.prototype.close = function(){
	if(this.isOpen == true){
		this.window.close();
		// unloadが働かない事を考えて
		this.isOpen = false;
	}
}

//! windowを閉じて開く
EspEditorBase.prototype.reopen = function(){
	this.close();
	this.open();
}

//! windowが開いていたら再描画する
EspEditorBase.prototype.redrawIfOpen = function(){
	if(this.isOpen == true){
		this.redraw();
	}
}

//! windowをリサイズする
EspEditorBase.prototype.resize = function() {
	this.window.resizeTo(this.width, this.height);
}
EspEditorBase.prototype.resizeTo = function(width, height) {
	this.width = width;
	this.height = height;
	this.window.resizeTo(width, height);
}

